<?php
namespace app\admin\controller;
use app\model\Admin as AdminModel;
use think\Db;


class Index extends Base
{
    public function index()
    {
        $page_info = $this->page_info;
        $page_info['title'] = '首页';

        $menu_list = [

            ['name'=>'用户','icon'=>'mdi mdi-account','url'=>'','childs'=>[
                ['name'=>'列表','url'=>url('user/index'),'title'=>'用户'],
                ['name'=>'新增','url'=>url('user/add'),'title'=>'用户'],
                ['name'=>'账单','url'=>url('user_bill/index')],
            ]],
        
            ['name'=>'来源','icon'=>'mdi mdi-foot-print','url'=>url('collect/index')],
            
            
            ['name'=>'产品','icon'=>'mdi mdi mdi-atom','url'=>'','childs'=>[
                ['name'=>'分类','url'=>url('category/index')],
                ['name'=>'列表','url'=>url('product/index')],
                ['name'=>'新增','url'=>url('product/add')],
            ]],



            ['name'=>'可灵AI任务','icon'=>'mdi mdi-newspaper-variant-outline','url'=>url('klingai_record/index')],
            ['name'=>'快手万擎任务','icon'=>'mdi mdi-newspaper-variant-outline','url'=>url('streamlake_record/index')],
            ['name'=>'火山引擎任务','icon'=>'mdi mdi-newspaper-variant-outline','url'=>url('volcengine_record/index')],

      

            // ['name'=>'运营管理','icon'=>'mdi mdi-language-swift','url'=>'','childs'=>[
            //     ['name'=>'合同管理','url'=>url('contract/index')],
            //     ['name'=>'发票管理','url'=>url('invoice/index')],
            //     ['name'=>'授权管理','url'=>url('authorization/index')],
            //     ['name'=>'实例管理','url'=>url('instance/index')],
            // ]],

            // ['name'=>'余额充值','icon'=>'mdi mdi-cash-multiple','url'=>url('recharge/index')],

            // ['name'=>'统计报表','icon'=>'mdi mdi-chart-areaspline','url'=>'','childs'=>[
            //     ['name'=>'统计概览','url'=>url('statistics/index')],
            //     ['name'=>'产品占比','url'=>url('statistics/index2')],
            // ]],

        ];
        if ( adminCheck() ) {
            array_unshift($menu_list, ['name'=>'管理员','icon'=>'mdi mdi-robot','url'=>'','childs'=>[
                ['name'=>'列表','url'=>url('admin/index'),'title'=>'管理员'],
                ['name'=>'新增','url'=>url('admin/add'),'title'=>'管理员']
            ]]);
            $menu_list[] = ['name'=>'系统设置','icon'=>'mdi mdi-cog','url'=>url('config/index')];
        }


        return view('index', [
            'page_info'=>$page_info,
            'menu_list'=>$menu_list
        ]);
    }

    public function main(){
        $data = [
            'klingai_total'=>Db::name('klingai_record')->count(),
            'user_total'=>Db::name('user')->count(),
        ];
        
        return view('main', [
            'data'=>$data,
        ]);
    }



    // 近一周新增咨询
    // public function newsLines(){
    //     $start_time = strtotime('-7 days');
    //     $end_time = strtotime('-1 days');
    //     $list = [];
    //     while ($start_time <= $end_time) {
    //         $stime = date('Y-m-d',$start_time);
    //         $list[] = [
    //             'date'=>date('m-d',$start_time),
    //             'value'=>Db::name('statistics')->where('ctime',$stime)->sum('customer_num')
    //         ];

    //         $start_time = strtotime('+1 days',$start_time);
    //     }
        
    //     return json([
    //         'code'=>2000,
    //         'data'=>['labels'=>array_column($list,'date'),'values'=>array_column($list,'value')]
    //     ]);
    // }
    public function klingaiLines(){
        $curdate = strtotime(date('Y-m-d'));
        $start_time = strtotime('-7 days',$curdate);
        $end_time = strtotime('-1 days',$curdate);
        $list = [];
        while ($start_time <= $end_time) {
            $stime = date('Y-m-d',$start_time);
            $list[] = [
                'date'=>date('m-d',$start_time),
                'value'=>Db::name('klingai_record')->where('ctime','between',[$start_time,$start_time + (24 * 3600) -1])->count()
            ];

            $start_time = strtotime('+1 days',$start_time);
        }
        
        return json([
            'code'=>2000,
            'data'=>['labels'=>array_column($list,'date'),'values'=>array_column($list,'value')]
        ]);
    }

    public function prompt($msg=''){
        return view('prompt', [
            'msg'=>$msg,
        ]);
    }

    // 坐席使用说明
    public function seats(){
        $page_info = $this->page_info;
        $page_info['title'] = '坐席说明';


        return view('seats', [
            'page_info'=>$page_info,
        ]);
    }

    // 获取消息列表
    public function news(){
        $ulogin = $this->login_user;
        $result = [
            'news'=>[]
        ];
 
        $hjzx = Db::name('hjzx_record')->field('id,customer_id,callee,bill')->where([
            ['sale_id','=',$ulogin['id']],
            Db::raw('handle_status = 2 or (handle_status != 10 and ctime >= '.(time() - 600).')')
        ])->order('id','desc')->select();
        foreach ($hjzx as $k => $vo) {
            $customer = Db::name('customer')->field('name')->where('id',$vo['customer_id'])->find();

            $result['news'][] = [
                'type'=>'hjzx',
                'id'=>$vo['id'],
                'content'=>'呼叫跟进：'.$customer['name'].'('.$vo['callee'].')，通话时长：'.$vo['bill'].'秒'
            ];
        }
        
        $result['nums'] = ['hjzx'=>count($hjzx)];
        $result['total'] = array_sum($result['nums']);

        return json(['code'=>2000,'data'=>$result]);
    }
    
    // 清空消息
    public function clearNews(){
        $ulogin = $this->login_user;
        Db::name('hjzx_record')->where([
            ['sale_id','=',$ulogin['id']],
            ['handle_status','<>',10]
        ])->update(['handle_status'=>10]);

        return json(['code'=>2000]);
    }

   
}
